package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.User;
import com.qk.exception.BusinessException;
import com.qk.mapper.UserMapper;
import com.qk.service.UserService;
import com.qk.dto.UserDto;
import com.qk.utils.JwtUtils;
import com.qk.vo.LoginResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    //查询
    @Override
    public PageResult<User> list(UserDto userDto) {
        //1.设置PageHelper 参数分页
        PageHelper.startPage(userDto.getPage(),userDto.getPageSize());
        //2.调用Mapper方法进行查询
        List<User> userlist =userMapper.selectList(userDto);
        //3.解析并封装结果
        Page<User> p = (Page<User>)userlist;
        return new PageResult<>(p.getTotal(),p.getResult());
    }
    //删除
    @Override
    public void deleteByIds(List<Integer> ids) {
        userMapper.deleteByIds(ids);
    }

    //添加
    @Override
    public void add(User user) {
        //补全基础属性
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        //设置默认密码
        String password = user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        //调用Mapper方法添加
        userMapper.insert(user);
    }
    //根据ID查询
    @Override
    public User getById(Integer id) {
        User user = userMapper.selectById(id);
        return user;
    }
    //修改用户
    @Override
    public void update(User user) {
        //补全更新时间属性
        user.setUpdateTime(LocalDateTime.now());
        //调用Mapper方法修改
        userMapper.update(user);
        //重新设置密码
        String password = user.getUsername()+"123";
        user.setPassword(DigestUtils.md5DigestAsHex(password.getBytes()));
        userMapper.update(user);
    }

    //查询所有用户
    @Override
    public List<User> listAll() {
        return userMapper.listAll();
    }
    //根据角色查询用户
    @Override
    public List<User> listByRole(String roleLabel) {
        //调用Mapper方法
        return userMapper.listByRole(roleLabel);
    }

    //根据部门查询用户
    @Override
    public List<User> listByDept(Integer deptId) {
        return userMapper.listByDept(deptId);
    }
    //登录
    @Override
    public LoginResultVo login(User user) {
        //1.调用Mapper方法
         User u=userMapper.selectByUsername(user.getUsername());
        //2.判断用户是否存在
        if(u==null){
            log.info("用户不存在");
            throw new BusinessException("用户名或密码错误");
        }

        //3.判断用户密码是否正确
        String pass=DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
        if(!pass .equals(u.getPassword())){
            log.info("用户密码错误");
            throw new BusinessException("用户名或密码错误");
        }

        //4.判断用户状态是否为1
        if (u.getStatus()!=1) {
            log.info("用户状态错误");
            throw new BusinessException("用户已停用");
        }
        //5.返回登录结果
        LoginResultVo loginResultVo = new LoginResultVo();
        loginResultVo.setId(u.getId());
        loginResultVo.setUsername(u.getUsername());
        loginResultVo.setName(u.getName());
        loginResultVo.setImage(u.getImage());
        loginResultVo.setRoleLabel(u.getRoleLabel());
        //6.生成令牌
        Map<String, Object>  claims=new HashMap<>();
        claims.put("id",u.getId());
        claims.put("username",u.getUsername());
        String jwt= JwtUtils.generateToken(claims);
        loginResultVo.setToken(jwt);

        log.info("用户登录成功");
        return loginResultVo;
    }
}
